package com.jevons.muffin.adapter;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * RecyclerView 万能适配器
 * @author linaisheng
 * Created at 2018/12/11.
 */

public abstract class QuickAdapter<T> extends RecyclerView.Adapter<QuickAdapter.QuickVH> {

    protected List<T> mList;

    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public QuickAdapter(List<T> list) {
        this.mList = list;
    }

    public abstract int getLayoutId(int viewType);

    @Override
    public QuickVH onCreateViewHolder(ViewGroup parent, int viewType) {
        final QuickVH vh = QuickVH.get(parent, getLayoutId(viewType));
        return vh;
    }

    @Override
    public void onBindViewHolder(final QuickVH holder, final int position) {
        if (onItemClickListener != null) {
            holder.mConvertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(holder.mConvertView, position);
                    }
                }
            });
            holder.mConvertView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (onItemLongClickListener != null) {
                        onItemLongClickListener.onItemLongClick(holder.mConvertView, position);
                    }
                    return false;
                }
            });
        }
        convert(holder, mList.get(position), position);
    }

    public T getItem(int position){
        return mList.get(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public List<T> getData() {
        return mList;
    }



    public abstract void convert(QuickVH holder, T data, int position);

    /**
     * 点击监听
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    /**
     * 长按监听
     * @param listener
     */
    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.onItemLongClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }


    //配合万能适配器使用的ViewHolder
    public static class QuickVH extends RecyclerView.ViewHolder {
        //保存的布局中View
        private SparseArray<View> mViews;
        //整个View布局
        View mConvertView;

        public QuickVH(View itemView) {
            super(itemView);
            mConvertView = itemView;
            mViews = new SparseArray<>();
        }

        public static QuickVH get(ViewGroup parent, int layoutId) {
            View convertView = LayoutInflater.from(parent.getContext())
                    .inflate(layoutId, parent, false);
            return new QuickVH(convertView);
        }

        public <T extends View> T getView(int id) {
            View v = mViews.get(id);
            if (v == null) {
                v = mConvertView.findViewById(id);
                mViews.put(id, v);
            }
            return (T)v;
        }

        /**
         * 设置TextView的文字
         * @param id
         * @param value
         */
        public void setText(int id, String value) {
            TextView textView = getView(id);
            textView.setText(value);
        }

    }

}
